package cn.b.sky.user.controller;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.ResourceService;
import cn.b.sky.user.service.model.ECode;
import cn.b.sky.user.service.model.ResourceEntity;
import cn.b.sky.user.service.model.condition.ResourceCondition;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @ClassName: ResourceController
 * @Description:资源管理控制器
 * @author: ZhaoXin
 * @date: 2015-7-28 上午11:12:46
 * @version: V1.0
 * 
 */
//@Controller
//@RequestMapping(value = "/resource/*")
public class ResourceController extends SecurityController {
  @Resource
  private ResourceService resourceService;

  @Resource
  private ECodeService eCodeService;

  @RequestMapping("resourceList")
  public String resourceList(Model model, @ModelAttribute("condition") ResourceCondition resource, String navTabId,
                             HttpSession httpSession) {

    resource.setLanguageCode(SecurityContextUtil.getLanguageCode());
    resource.seteCodeType(ECodeEnums.RESOURCETYPEECODE.getCode());
    List<ResourceEntity> resources = resourceService.selectResourceList(resource);
    int count = resourceService.selectResourceCount(resource);
    List<ECode> types = eCodeService.selectECodeList(ECodeEnums.RESOURCETYPEECODE.getCode(),
        SecurityContextUtil.getLanguageCode());
    resource.setData(resources);
    resource.setTotalCount(count);
    model.addAttribute("page", resource);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("resourceType", types);
    return "resource/resource_list";
  }

  @RequestMapping("addResource")
  public String addService(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    List<ECode> types = eCodeService.selectECodeList(ECodeEnums.RESOURCETYPEECODE.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("resourceType", types);
    return "resource/resource_add";
  }

  @RequestMapping("modifyResource")
  public String modifyResource(Model model, String navTabId, String id) {
    ResourceEntity resource = resourceService.selectResource(id);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("resource", resource);
    List<ECode> types = eCodeService.selectECodeList(ECodeEnums.RESOURCETYPEECODE.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("resourceType", types);
    return "resource/resource_add";

  }

  @RequestMapping("viewResource")
  public String viewResource(Model model, String navTabId, String id) {

    ResourceEntity resource = resourceService.selectResource(id);
    List<ECode> types = eCodeService.selectECodeList(ECodeEnums.RESOURCETYPEECODE.getCode(),
        SecurityContextUtil.getLanguageCode());
    model.addAttribute("resourceType", types);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("resource", resource);
    return "resource/resource_view";
  }

  @RequestMapping("saveResource")
  @ResponseBody
  public DWZResult saveResource(Model model, String navTabId, String id, ResourceEntity resource,
                                HttpServletRequest request) {
    if (resource == null) {

      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    setCreateInfo(resource);
    if (StringUtils.isEmpty(id)) {
      resourceService.insertResource(resource);
      String forwardUrl = getForwardUrl(request) + "/resource/resourceList?navTabId=" + navTabId;
      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          navTabId, null, forwardUrl, null);
    } else {
      resourceService.updateResource(resource);
      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null, null, null, null);
    }

  }

  @RequestMapping("validateResourceCode")
  @ResponseBody
  public boolean validateResourceCode(String resourceCode, Model model) {
    boolean isValid = true;
    if (!StringUtils.isBlank(resourceCode)) {
      ResourceEntity resourceEntity = resourceService.selectResource(resourceCode);
      if (resourceEntity != null) {
        isValid = false;
      }
    }
    return isValid;

  }

  @RequestMapping("deleteResource")
  @ResponseBody
  public DWZResult deleteResource(Model model, String navTabId, String id, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isEmpty(ids)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    String[] ress = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(ress.length);
    boolean hasFail = false;
    for (String res : ress) {
      BatchJobReport report = resourceService.deleteResource(res);
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      // 直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone( request, reports);
    }

  }

  @RequestMapping("getResourceType")
  @ResponseBody
  public String getResourceType(Model model, String navTabId, HttpServletRequest request) {
    List<ECode> types = eCodeService.selectECodeList(ECodeEnums.RESOURCETYPEECODE.getCode(),
        SecurityContextUtil.getLanguageCode());
    StringBuilder sbTypes = new StringBuilder();
    if (types != null) {
      for (ECode type : types) {
        sbTypes.append(",[");
        sbTypes.append("\"" + type.geteCode() + "\",");
        sbTypes.append("\"" + type.geteCodeText() + "\"");
        sbTypes.append("]");
      }
    }
    return sbTypes.toString();

  }

}
